package DBLayer;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.*;

public class DBConnection
{   //access information for the database
	private static final String  driver = "jdbc:sqlserver://balder.ucn.dk:1433";
	private static final String  databaseName = ";databaseName=dmaa0912_4";
	private static String  userName = "; user=dmaa0912_4";
	private static String password = ";password=MaaGodt";


	private DatabaseMetaData dma;
	private static Connection con;
	// creates an instance of the class DBConnection
	private static DBConnection  instance = null;

	//constructor for DBConnection is private to ensure singleton access for other classes
	private DBConnection()
	{
		String url = driver + databaseName + userName + password;

		try{
			//Load the SQLServerDriver
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			System.out.println("loaded SQLServerDriver ok");

		}//end try
		catch(Exception e){
			System.out.println("Can not find the driver");
			System.out.println(e.getMessage());
		}//end catch

		try{
			//connection to the database
			con = DriverManager.getConnection(url);
			//set autocommit
			con.setAutoCommit(true);
			dma = con.getMetaData(); // get meta data
			System.out.println("Connection to " + dma.getURL());
			System.out.println("Driver " + dma.getDriverName());
			System.out.println("Database product name " + dma.getDatabaseProductName());
		}//end try
		catch(Exception e){

			System.out.println("Problems with the connection to the database");
			System.out.println(e.getMessage());
			System.out.println(url);
		}//end catch
	}//end  constructor

	//closeDB: closes the connection to the database
	public static void closeConnection()
	{
		try{
			con.close();
			System.out.println("The connection is closed");
		}
		catch (Exception e){
			System.out.println("Error trying to close the database " +  e.getMessage());
		}
	}//end closeDB

	//getDBcon: Get-metode, returns connection to the database
	public  Connection getDBcon()
	{
		return con;
	}
	//this method is used to get the instance of the connection
	public static DBConnection getInstance()
	{
		if (instance == null)
		{
			instance = new DBConnection();
		}
		return instance;
	}
	public static void startTransaction()
	{ try{
		con.setAutoCommit(false);
	}
	catch(Exception e){
		System.out.println("error start transaction");
		System.out.println(e.getMessage());
	}
	}
	public static void commitTransaction()
	{ try{
		con.setAutoCommit(true);
	}
	catch(Exception e){
		System.out.println("error commit transaction");
		System.out.println(e.getMessage());
	}
	}
	public static void rollbackTransaction()
	{ try{
		con.rollback();
		con.setAutoCommit(true);
	}
	catch(Exception e){
		System.out.println("error rollback transaction");
		System.out.println(e.getMessage());
	}
	}
}//end DBConnection
